<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\user_center\logic\user;
use think\facade\Hook;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class SmsSendlogic{
	
	
    private $keyId           = 'LTAI4G9KPip2LSwWXN7XxcYe';//阿里云短信accessKeyId
    private $keyScret        = 'TVsIcAtwpteTVarJe4T1JqbkJD218x';//阿里云短信accessKeySecret  
    private $signName        = '幕小铺';//短信签名
	
	/**
     * 发送短信
	 * @param  int $mobile  		手机号
	 * @param  int $code  			验证码
	 * @param  int $template_code   短信模板code
     * @author shiqiang <976162041@qq.com>
     * @return mixed
     */
	public function sendSms($mobile, $code, $template_code){
		if($mobile >= 14400000001 && $mobile <= 14499999999){
			return array_out(200, '发送成功');
		}
		//阿里云发送短信
		AlibabaCloud::accessKeyClient($this -> keyId, $this -> keyScret)
                    ->regionId('cn-hangzhou') //replace regionId as you need（这个地方是发短信的节点，默认即可，或者换成你想要的）
                    ->asGlobalClient();
		try {
			$result = AlibabaCloud::rpcRequest()
					  ->product('Dysmsapi')
					  ->scheme('https') //https | http（如果域名是https，这里记得开启）
					  ->version('2017-05-25')
					  ->action('SendSms')
					  ->method('POST')
					  ->options([
							'query'                 => [
								'PhoneNumbers'      => $mobile,
								'SignName'          => $this -> signName,
								'TemplateCode'      => $template_code,
								'TemplateParam'     => json_encode(['code'=>$code]),
							],
						])
					  ->request();
					  
			$res = $result->toArray();
			if($res['Code'] == 'OK'){
				return array_out(200, $res['Message']);
			}else{
				return array_out(-1001, $res['Message']);
			}
		} catch (\ClientException $e) {
			return array_out(-1001, $e->getErrorMessage());
		} catch (\ServerException $e) {
			return array_out(-1001, $e->getErrorMessage());
		}
	}
}